package com.itheima.controller.sms;

import com.itheima.core.PageInfo;
import com.itheima.core.R;
import com.itheima.pojo.sms.SmsCoupon;
import com.itheima.pojo.sms.vo.SmsCouponVo;
import com.itheima.service.sms.SmsCouponService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/coupon")
public class SmsCouponController {

    @DubboReference
    private SmsCouponService couponService;

    /**
     * 添加优惠券
     * @param couponVo
     * @return
     */
    @PostMapping("/create")
    private R createCoupon(@RequestBody SmsCouponVo couponVo){
        couponService.createCoupon(couponVo);
        return R.success();
    }

    /**
     * 删除优惠券
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R deleteCoupon(@PathVariable Integer id){
        couponService.deleteCoupon(id);
        return R.success();
    }

    /**
     * 根据优惠券名称和类型分页获取优惠券列表
     * @param name
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R listCoupon(@RequestParam(required = false) String name,@RequestParam(required = false) Integer pageNum,
                        @RequestParam(required = false) Integer pageSize,@RequestParam(required = false) Integer type){
        PageInfo pageInfo = couponService.listCoupon(name,pageNum,pageSize,type);
        return R.success(pageInfo);
    }

    /**
     * 修改优惠券
     * @param couponVo
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateCoupon(@RequestBody SmsCouponVo couponVo, @PathVariable Integer id){
        couponService.updateCoupon(couponVo,id);
        return R.success();
    }

    /**
     * 获取单个优惠券的详细信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getOneCoupon(@PathVariable Integer id){
        SmsCouponVo couponVo = couponService.getOneCoupon(id);
        return R.success(couponVo);
    }
}
